define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon.user/index',
                    // add_url: 'coupon.user/add',
                    // edit_url: 'coupon.user/edit',
                    // del_url: 'coupon.user/del',
                    // multi_url: 'coupon.user/multi',
                    send_url: 'coupon.user/send',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'nickname', title: __('Username'), operate: 'LIKE' },
                        { field: 'mobile', title: __('手机号'), operate: 'LIKE' },
                        { field: 'avatar', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: { normal: __('Normal'), hidden: __('Hidden') } },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'send',
                                    title: '发放',
                                    text: '发放',
                                    classname: 'btn btn-primary btn-xs btn-view btn-ajax',
                                    url: 'coupon.user/send?coupon_id='+Config.id,//接口地址 控制器名/方法名
                                },
                            ]
                        }
                    ]
                ]
            });
            $(document).on("click", ".btn-opera", function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                var coupon_id = Config.id;
                Layer.confirm(
                    __('确定一键操作?', ids.length),
                    {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                    function (index) {
                        Fast.api.ajax({
                            url: "coupon.user/send",
                            type: "post",
                            data: {ids:ids.toString(),coupon_id:coupon_id},
                        }, function () {
                        });
                        Layer.close(index);
                        table.bootstrapTable('refresh', {}); //关闭刷新
                    }
                );
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});